package edu.hnnu.sport_event.controller;

import edu.hnnu.sport_event.entity.Event;
import edu.hnnu.sport_event.entity.Student;
import edu.hnnu.sport_event.service.EventService;
import edu.hnnu.sport_event.service.SignUpService;
import edu.hnnu.sport_event.utils.Result;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("event")
public class EventController {

    @Resource
    EventService eventService;
    @Resource
    SignUpService signUpService;

    @RequestMapping("add")
    // 添加运动项目
    public Result addEvent(Event event, @RequestParam("startTime") String time) throws ParseException {
        event.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm").parse(time));
        event.setParticipants(event.getQuota());
        int res = eventService.insert(event);
        return res == 1 ? Result.success("添加成功") : Result.error("添加失败");
    }

    @RequestMapping("getPage")
    // 获取运动项目（分页待实现）
    public List<Event> getEvents(Integer pn) {
        return eventService.selectAll(pn);
    }

    @RequestMapping("get")
    // 获取单个运动项目描述
    public Event getEvent(Integer id) {
        if (id == null) return null;
        return eventService.select(id);
    }

    @RequestMapping("del")
    // 删除运动项目
    public Result delEvent(int id) {
        if (eventService.delete(id) > 0) return Result.success("删除成功");
        return Result.error("删除失败");
    }

    @RequestMapping("modify")
    // 修改运动项目
    public Result modifyEvent(Integer id, Event event, String startTime) throws ParseException {
        event.setId(id);
        Date time = new SimpleDateFormat("yyyy-MM-dd HH:ss").parse(startTime);
        event.setTime(time);
        if (eventService.modify(event) > 0) return Result.success("修改成功");
        return Result.error("修改失败");
    }

    @RequestMapping("signup")
    // 项目报名
    public Result signUp(Integer studentId, Integer eventId) {
        if (null == studentId || null == eventId) {
            return Result.error("报名失败");
        }
        if (eventService.signUp(studentId, eventId) > 0) {
            return Result.success("报名成功").addAttribute("events", eventService.signUpEvents(studentId));
        }
        return Result.error("你已经报名该项目");
    }

    @RequestMapping("eventDetails")
    // 获取学生项目报名列表
    public Student getEventDetails(Integer studentId) {
        if (null == studentId) return null;
        return eventService.getEventDetails(studentId);
    }

    @RequestMapping("cancel")
    // 取消报名
    public Result cancelRegistry(Integer studentId, Integer eventId) {
        if (null == studentId || null == eventId) return Result.error("网络异常");
        int res = eventService.cancelRegistry(studentId, eventId);
        if (res > 0) return Result.success("报名取消成功");
        return Result.error("取消报名失败");
    }

    @RequestMapping("participants")
    // 获取运动项目的报名学生
    public Event getParticipants(Integer eventId) {
        if (null == eventId) return null;
        return eventService.getParticipants(eventId);
    }

    @RequestMapping("scoring")
    // 
    public Student scoringInfo(Integer id) {
        if (null == id) return null;
        return signUpService.ScoringInfo(id);
    }

    @RequestMapping("addScore")
    public Result modify(Integer id, Integer franction) {
        System.out.println(id + "  " + franction);
        if (signUpService.update(id, franction) > 0) return Result.success("修改成功");
        return Result.error("修改失败");
    }

}
